package com.zryc.shop.sms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zryc.shop.sms.entity.FlashPromotion;
import com.zryc.shop.sms.service.IFlashPromotionService;
import com.zryc.shop.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.time.LocalDateTime;

/**
 * <p>
 * 限时购表 前端控制器
 * </p>
 *
 * @author LiYe
 * @since 2023-02-13
 */
@RestController
@RequestMapping("/sms/flash")
@Api(tags = "秒杀活动管理接口列表")
public class FlashPromotionController {
    @Autowired
    private IFlashPromotionService flashPromotionService;

    @GetMapping("/list")
    @ApiOperation("分页列表")
    public ResponseResult list(@RequestParam(required = false) @ApiParam(value = "品牌名称关键词") String keyword,
                               @RequestParam(defaultValue = "1") @ApiParam(value = "页码", required = true) Integer pageNum,
                               @RequestParam(defaultValue = "5") @ApiParam(value = "每页显示条数", required = true) Integer pageSize) {
        Page page = new Page(pageNum, pageSize);
        QueryWrapper<FlashPromotion> queryWrapper = new QueryWrapper();
        if (!StringUtils.isEmpty(keyword)) {
            queryWrapper.like("title", keyword);
        }
        flashPromotionService.page(page, queryWrapper);
        return new ResponseResult(200, "查询完成", page);
    }

    @PostMapping("/create")
    @ApiOperation("添加秒杀活动")
    public ResponseResult create(@RequestBody FlashPromotion flashPromotion) {
        flashPromotion.setCreateTime(LocalDateTime.now());
        flashPromotionService.save(flashPromotion);
        return new ResponseResult(200, "创建成功");
    }

    @PostMapping("/delete/{id}")
    @ApiOperation("删除秒杀活动")
    public ResponseResult delete(@PathVariable @ApiParam(value = "活动ID", required = true) Long id) {
        flashPromotionService.removeById(id);
        return new ResponseResult(200, "删除成功");
    }

    @PostMapping("/update/status/{id}")
    @ApiOperation("修改秒杀活动状态")
    public ResponseResult updateStatus(@PathVariable @ApiParam(value = "活动ID", required = true) Long id,
                                       @RequestParam @ApiParam(value = "活动状态: 0-关闭 1-开启", required = true, allowableValues = "0,1") Integer status) {
        UpdateWrapper<FlashPromotion> updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id", id);
        updateWrapper.set("status", status);
        flashPromotionService.update(updateWrapper);
        return new ResponseResult(200, "修改成功");
    }

    @PostMapping("/update/{id}")
    @ApiOperation("更新秒杀活动")
    public ResponseResult updateStatus(@PathVariable @ApiParam(value = "活动ID", required = true) Long id, @RequestBody FlashPromotion flashPromotion) {
        flashPromotion.setId(id);
        flashPromotionService.updateById(flashPromotion);
        return new ResponseResult(200, "修改成功");
    }
}
